package com.ccdt.tv.smartrecommend.view.tools;

import android.util.Log;

import com.blankj.utilcode.util.DeviceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wudz on 2017/11/20.
 * 正则替换参数中的关键字
 */

public class OutEvetnKeyword {
    private static final String TAG="OutEvetnKeyword";
    //时间正则关键字
    private static final String TIME="<(\\s+)?time(\\s+)?>";
    //mac正则关键字
    private static final String MAC="<(\\s+)?mac(\\s+)?>";
    //mac地址缓存
    private static String mac;
    public static String replaceKeyword(String originalStr)
    {
        try {
            String operationStr=originalStr;
            for (PatternAndRepalce pattern : getRetainPatterns()) {
                Matcher m=pattern.pattern.matcher(operationStr);
                Log.i(TAG,"m.find:"+m.find());
                operationStr=m.replaceAll(pattern.replace);
                Log.i(TAG,operationStr);
            }
            return operationStr;
        }catch (Exception e)
        {
            Log.i(TAG,"replaceKeyword:error!");
            e.printStackTrace();
            return originalStr;
        }
    }
    private static List<PatternAndRepalce> getRetainPatterns()
    {
        ArrayList<PatternAndRepalce> res=new ArrayList();
        res.add(new PatternAndRepalce(Pattern.compile(TIME),""+System.currentTimeMillis()));
        if(mac==null || mac.equals(""))
        {
            mac= DeviceUtils.getMacAddress();
            mac=mac.toUpperCase();
        }
        res.add(new PatternAndRepalce(Pattern.compile(MAC),mac));
        return res;
    }

    private static final class PatternAndRepalce
    {
        Pattern pattern;
        String replace;
        public PatternAndRepalce(Pattern pattern,String replace)
        {
            this.pattern=pattern;
            this.replace=replace;
        }

    }
}
